package br.com.knowledge.view.struts.actions;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.ResultPath;
import org.apache.struts2.interceptor.SessionAware;
import org.apache.struts2.interceptor.validation.SkipValidation;
import org.hibernate.Session;

import br.com.knowledge.persistence.hibernate.entities.FntObjeto;
import br.com.knowledge.persistence.hibernate.dao.FntObjetoDAO;

import com.opensymphony.xwork2.ActionSupport;

@Namespace("/")
@ResultPath("/")
public class ObjetoAction extends ActionSupport implements SessionAware{

	public void setSession(Map<String, Object> arg0) {
		// TODO Auto-generated method stub		
	}
	

	@Action(value = "cadastra-objeto", results = {
			@Result(name = SUCCESS, location = "/pages/listarTiposDados.jsp"),
			@Result(name = ERROR, location = "/pages/cadastraTipoDadoErro.jsp"),
			@Result(name = INPUT, location = "/pages/cadastraTipoDado.jsp") })	
	public static FntObjeto criarObjeto(FntObjeto objeto){
		FntObjetoDAO cDaoFntObjeto = new FntObjetoDAO();
		Session sessionFntObjeto = cDaoFntObjeto.getSession();		
		try{
			sessionFntObjeto.beginTransaction();
			cDaoFntObjeto.save(objeto);
			sessionFntObjeto.getTransaction().commit();
		}catch (Exception e) {
			// TODO: handle exception
			sessionFntObjeto.getTransaction().rollback();
			return null;
		}finally{
			sessionFntObjeto.close();
		}
		return objeto;
	}
	/*
	@Action(value = "cadastra-objeto", results = {
			@Result(name = SUCCESS, location = "/pages/listarTiposDados.jsp"),
			@Result(name = ERROR, location = "/pages/cadastraTipoDadoErro.jsp"),
			@Result(name = INPUT, location = "/pages/cadastraTipoDado.jsp") })	
	public static String criarObjeto(FntObjeto objeto){
		FntObjetoDAO cDaoFntObjeto = new FntObjetoDAO();
		Session sessionFntObjeto = cDaoFntObjeto.getSession();
		try{
			sessionFntObjeto.beginTransaction();
			cDaoFntObjeto.save(objeto);
			sessionFntObjeto.getTransaction().commit();
		}catch (Exception e) {
			// TODO: handle exception
			sessionFntObjeto.getTransaction().rollback();
		}finally{
			sessionFntObjeto.close();
		}
		return SUCCESS;
	}*/
	

	@Action(value = "listar-objeto", results = {
			@Result(name = SUCCESS, location = "/pages/listarTiposDados.jsp"),
			@Result(name = ERROR, location = "/pages/cadastraTipoDadoErro.jsp"),
			@Result(name = INPUT, location = "/pages/cadastraTipoDado.jsp") })	
	public String listarObjeto(){
		return SUCCESS;
	}
}
